{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "160773cf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b01cfa0c",
   "metadata": {},
   "source": [
    "列表的高级用法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ebb7a03",
   "metadata": {},
   "source": [
    "列表 取值☞1.index 索引取单个值 2.slice切片取一些值 还是一个列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83d3475e",
   "metadata": {},
   "source": [
    "在python中，一切都是对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7593b67",
   "metadata": {},
   "source": [
    "2.列表增加"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53141451",
   "metadata": {},
   "source": [
    "###  2.1 append() 增加元素到list的末位\n",
    "    append()一般用来新建列表会使用到\n",
    "    > for i in names:\n",
    "        if i[0] == \"J\":\n",
    "           names_J.append(i)\n",
    "      print(\"names_J=\",names_J)\n",
    "      print（names）\n",
    "      \n",
    "    2.2 insert（） 插入元素\n",
    "  \n",
    " \n",
    "### 3. 删除或移除\n",
    "    3.1 remove() 使用remove之前，应该先判断该元素是否存在该List当中 （用in的方式先去进行查询）（优势：我确定要删掉某人的信息，找到一个就可以删一个）\n",
    "    ctrl+/可以全部打开也可以全部删掉\n",
    "    3.2 pop() 删除 pop()不填内容 对应记忆 ==>append()删除末位 （pop就是删除末位）\n",
    "      \n",
    "### 4.index()\n",
    "### 本质：List 本身有index￥ value\n",
    "### 枚举 （把隐藏的你需要的列出来）\n",
    "### 5.sort ( ) 排序 默认从小到大排序 reverse =Fales(默认)小到大\n",
    "###                                                      reverse=True 大到小\n",
    "                  如：STU_成绩.sort(reverse=True)\n",
    "                     print(names)\n",
    "### 6.count()查看某元素的次数\n",
    "### 7.clear() 清空list\n",
    "     names.clear()\n",
    "     print(names)\n",
    "### 8.extand()是有顺序的，操作哪一个列表，那一个列表就先写\n",
    "\n",
    "##列表的真正意义的复制 \n",
    "### 1.假复制 用= 进行赋值 错误的复制列表操作，大家容易这么去做，请不要这么去做\n",
    "     names =[\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "     names_源文件 = names\n",
    "     names.append(\"moxiufang\")\n",
    "     print(\"names =\",names)\n",
    "     print(\"names_源文件 = \",names_源文件)\n",
    "### 2.正确的方法1.copy（）\n",
    "### 3.正确的方法2. 切片[:] 用切片实现列表的复制\n",
    "     names =[\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "     names_源文件 = names[:]\n",
    "     names.append(\"moxiufang\")\n",
    "     print(\"names =\",names)\n",
    "     print(\"names_源文件 = \",names_源文件)\n",
    " \n",
    "p67 \"Don't panic!\" => list => \"on tap\"\n",
    "str可以转为list list也可以转为str\n",
    "\n",
    "字符串的操作没那么容易，列表容易些\n",
    "extend两个列表拼在一起 运算顺序，（1×（5+1））\n",
    "\n",
    "List的方法会打乱和改变List顺序\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c45d1d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "names =[\"Mike\",\"Mary\"\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61927a73",
   "metadata": {},
   "source": [
    "name = [],注意这里是方括号"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87a8328c",
   "metadata": {},
   "source": [
    "+= 就是+1 可以写+1，也可以写+=\n",
    "\n",
    "random是0-1随机数\n",
    "\n",
    "randint可以设置范围"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0e6eaa8",
   "metadata": {},
   "source": [
    "列表的方法是会改变原来的方法的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20f0058e",
   "metadata": {},
   "source": [
    "### 列表练习 初始字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "421e1b6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n",
      "-----------ke-------------\n",
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n",
      "-------------tuple------------\n",
      "(12345, 54321, 'hello!')\n",
      "<class 'tuple'>\n",
      "time.struct_time(tm_year=2021, tm_mon=11, tm_mday=12, tm_hour=1, tm_min=12, tm_sec=24, tm_wday=4, tm_yday=316, tm_isdst=0)\n"
     ]
    }
   ],
   "source": [
    "# 目标： “Don't panic!” ==> \"on tap\"\n",
    "# 方法一： 使用list的方法完成\n",
    "# 1.课本方法\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase) # list() 可以将str to(转） list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "# 1.删除后面四个元素n i c ！\n",
    "for i in range(4): # for循环删除最后一个对象四次  “nic！”\n",
    "    plist.pop() # 删除最后一个对象\n",
    "print(plist)\n",
    "\n",
    "# 2.删除 D '\n",
    "\"\"\"\n",
    "plist.pop(0) # 删除第一个对象“D”\n",
    "print(plist)\n",
    "plist.remove(\"'\")\n",
    "print(plist) # 删除 特殊符号\n",
    "\"\"\"\n",
    "plist.remove(('D'))\n",
    "print(plist)\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "\n",
    "# 3. 数学案例： 计算 （(a+b)*(c+d)\n",
    "plist.extend([plist.pop(),plist.pop()]) # 交换列表末尾两个对象 \"ont ap\"\n",
    "#   ['o', 'n', 't', ' ',] [    a   ,       p     ]\n",
    "print(plist)\n",
    "\n",
    "# 4. 数据插入  实现：'t' 和 ' ' 的位置转换\n",
    "\n",
    "plist.insert(2,plist.pop(3)) #  “on tap”\n",
    "print(plist)\n",
    "plist.insert(2,plist.pop(3)) # ont ap\n",
    "print(plist)\n",
    "plist.insert(2,plist.pop(3)) # on tap\n",
    "\n",
    "\n",
    "# 5. list to str\n",
    "new_plist = \"\".join(plist)  # 将列表转换成字符串\n",
    "print(plist)\n",
    "print(new_plist)\n",
    "print(\"-----------ke-------------\")\n",
    "#  列表的方法会改变列表本身， 如果不想打破或者想保留原先数据的内容，请用\n",
    "\n",
    "# 方法二： 使用切片的方法实现\n",
    "\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "on_str =''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str+ ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "print(plist)\n",
    "print(new_phrase)\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "new_phrase = (\"\".join(plist[1:3])) + \\\n",
    "    (\"\".join([plist[5], plist[4], plist[7], plist[6]]))\n",
    "print(new_phrase)\n",
    "\"\"\"\n",
    "# 对比panic.py 和 panic2.py\n",
    "# panic.py 使用列表的方法来处理 而 panic2.py 用的是切片的方法\n",
    "# 列表的方法会改变一个列表的原始状态，而切片通常不会改变列表的状态\n",
    "\n",
    "print(\"-------------tuple------------\")\n",
    "##        知识点： 元组tuple\n",
    "# 元组是不可变的； 由多个以逗号分隔的值组成\n",
    "t = 12345, 54321, 'hello!'\n",
    "print(t)\n",
    "print(type(t))\n",
    "\n",
    "\n",
    "import time\n",
    "print(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77e92999",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
